package games.mapacman.client;

import games.mapacman.common.Constants;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MaPacmanGui extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7777348013177012861L;
	private static MaPacman client;
	private JTextField sendText;
	private JPanel panel;
	private LogPanel loginpanel;
	public GameScreen screen;

	public MaPacmanGui(MaPacman machatclient) {
		client = machatclient;
		//loginpanel = new LogPanel(client);
		// load Icon
		Toolkit tk = this.getToolkit();
                Image iconimage = null;
                try{
		iconimage = tk.createImage(MaPacmanGui.class
				.getResource("/games/mapacman/client/images/Icon.png"));
                } catch (Exception exception){
                    exception.printStackTrace();
                }
                while (!tk.prepareImage(iconimage, -1, -1, this)) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {
			}
		}
		setIconImage(iconimage);

		this.setSize(600, 450);
		this.setTitle("JMaPacMan " + Constants.VERSION);
		this.setLocationRelativeTo(null);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				client.close();
			}
		});

		this.setLayout(new BorderLayout(1, 2));
		//this.add(loginpanel, BorderLayout.CENTER);
                //this.getContentPane().add(loginpanel);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				client.stopGame();
			}
		});

	}

	public GameScreen prepareScreen() {
		// this.remove(loginpanel);
		panel = new JPanel();
		sendText = new JTextField("", 25);
		sendText.addKeyListener(new PanelKeyListener(client));
		ActionListener sendclick = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				client.sendChatMsg(sendText.getText());
				sendText.setText("");
			}
		};
		sendText.addActionListener(sendclick);
		this.add(panel, BorderLayout.CENTER);
		this.add(sendText, BorderLayout.SOUTH);
		this.validate();
		sendText.requestFocus();

		panel.setLayout(null);

		// setup our canvas size and put it into the content of the frame
		Canvas canvas = new Canvas();
		canvas.setBounds(0, 0, 600, 400);
		// Tell AWT not to bother repainting our canvas since we're
		// going to do that our self in accelerated mode
		//canvas.setIgnoreRepaint(true);
		panel.add(canvas);
		BufferStrategy strategy;
		canvas.createBufferStrategy(2);
		strategy = canvas.getBufferStrategy();

		canvas.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				sendText.requestFocus();
			}

			public void focusLost(FocusEvent e) {
			}
		});

		GameScreen.createScreen(strategy, 600, 355, 155, 155, panel);
		screen = GameScreen.get();

		return screen;
	}

	public void printText(String text) {
		if (screen != null) {
			screen.printChatText(text);
		} else {
			System.out.println(text);
		}
	}

	public void ErrorMsg(String text) {
		JOptionPane.showMessageDialog(this, text, "Error",
				JOptionPane.WARNING_MESSAGE);
	}

	public GameScreen getScreen() {
		return screen;
	}
}
